<template>
    <rx-layout>
        <template v-slot:center>
            <RxLoading v-if="Loading"></RxLoading>
            <rx-form v-else-if="formJs.data" ref="rxForms" :action="action"
                     :context-data="contextData"
                     :formJs="formJs" :resForm="resForm"></rx-form>
        </template>
        <template v-slot:footer>
            <bpm-detail-buttons :curVm="getCurVm()"></bpm-detail-buttons>
        </template>
    </rx-layout>
    <!-- 提示窗示例 -->
    <uni-popup ref="alertDialog" type="dialog">
        <uni-popup-dialog
            :type="popupConfig.msgType" :cancelText="i18n('close','取消')" :confirmText="i18n('ok','确定')" :title="popupConfig.title" :content="popupConfig.content"
            @confirm="dialogConfirm(popupConfig)"
        ></uni-popup-dialog>
    </uni-popup>
    <!-- 提示信息弹窗 -->
    <uni-popup ref="message" type="message">
        <uni-popup-message :type="msgConfig.msgType" :message="msgConfig.messageText" :duration="2000"></uni-popup-message>
    </uni-popup>
</template>

<script>
import RxForm from "../../../views/modules/form/customForm/RxForm"
import RxLayout from '@/components/RxLayout'
import Util from '@/js/Util.js'
import BpmInstApi from "../../../api/rxBpm/bpmInst";
import BpmInstTrackedApi from "../../../api/rxBpm/bpmInstTracked";
import BpmDetailButtons from "./BpmDetailButtons"
import formBase from './js/formBase.js';
import Dialog from "@/js/Dialog";
import RxLoading from '@/components/RxLoading'


export default {
    name: "BpmInstDetail",
    components: {
        RxForm,
        RxLayout,
        BpmDetailButtons,
        RxLoading
    },
    mixins: [formBase],
    props: {
        instId: {
            type: String,
            required: true
        },
        backNodeId: {//在流程明细中进行回退时指定的目标节点
            type: String,
            required: false
        },
        allowTransfer: {
            type: Boolean,
            required: false,
            default: false
        },
        allowTransReply: {
            type: Boolean,
            required: false,
            default: false
        },
        //来自页面(mydone:我完成的审批,mystart:我发起的流程)
        from: {
            type: String,
            default: "mydone"
        },
        appId: String,
    },
    computed: {
        trackedTheme() {
            return this.bpmInstDetail.tracked == '1' ? 'filled' : 'outlined';
        },
        trackText() {
            return this.bpmInstDetail.tracked == "1" ? this.i18n('Tracked','已跟踪') :this.i18n('track','跟踪') ;
        },
        canTrack() {
            return this.bpmInst.status == "RUNNING" || this.bpmInst.status == 'REVOKE';
        },

        showMsgBtn() {
            let ary = ['ARCHIVED'];
            if (ary.includes(this.bpmInst.status)) {
                return false;
            }
            return true;
        }
    },
    data() {
        return {
            msgConfig:{
                msgType:'',
                messageText:""
            },
            popupConfig:{
                method:'',
                title:this.i18n('tips','提示信息'),
                msgType:'warn',
                content:this.i18n('suspend','您确定暂停当前流程吗?')
            },
            historyShow: false,
            bpmCommuBtnShow: false,
            bpmCommuShow: false,
            logShow: false,
            msgShow: false,
            bpmInstDetail: {},
            bpmInst: {},
            processConfig: {
                buttons: [],
                startConfirm: false,
                fillOpinion: false,
                assignFlowUsers: false,
                startCalFlowusers: false,
                //是否允许选择路径。
                allowSelectPath: false,

                formType: "online",
                //开关
                switchOptions: [],
            },
            buttons: [],
            pkId: "",
            formComponent: "",
            outSideUrl: "",
            urlParamData: [],
            urlParamsShow: false,
            pk: "",
            //根实例
            rootVm: true,
            //是否可以撤回
            canRevoke: false,
            myStartBtn: [],
            //按钮loading
            show: true,
            formData: {},
            isShowImage: false,
            formJs: {},
            action: 'detail',//状态edit编辑，detail明细
            resForm: {},
            contextData: {},
            Loading:true,
        }
    },
    created() {
        this.loadDetail();
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"bpmTaskLang.bpmOther",key);
        },
        loadDetail() {
            let self = this;
            if (!this.instId) {
                return
            }
            let curUser = uni.getStorageSync("user")
            var self_ = this;
            BpmInstApi.getInstDetail(this.instId, this.from).then(res => {
                self_.Loading=false;
                this.bpmInstDetail = res;
                this.bpmInst = res.bpmInst;
                var contextData = {
                    type: "detail",
                    defId: res.bpmInst.defId,
                    instNo: res.bpmInst.instNo,
                    subject: res.bpmInst.subject,
                    instId: self.instId,
                    opinionHistorys: res.bpmCheckHistories,
                    //当前是否为发起人
                    starter:this.bpmInst.createBy==curUser.userId,
                };
                //设置启动标题.
                uni.setNavigationBarTitle({
                    title: this.bpmInst.subject
                });
                //上下文数据
                this.contextData = contextData;

                var config = Util.deepClone(self_.processConfig);
                self_.processConfig = Object.assign(config, res.processConfig);
                self_.buttons = self_.processConfig.buttonDetailConfigs;
                self_.canRevoke = res.canRevoke;
                self.pkId = res.bpmInst.busKey;
                var config = Util.deepClone(self.processConfig);
                self.processConfig = Object.assign(config, res.processConfig);
                if (!self.processConfig.formType) {
                    self.processConfig.formType = "online"
                }
                let formData = res.formData;
                this.resForm = formData.data[0];
                if(this.resForm.globalConfig){
                    uni.setStorage({key: "globalConfig", data: JSON.parse(this.resForm.globalConfig)})
                }else {
                    uni.setStorage({key: "globalConfig", data: {}})
                }
                let scriptJs = Util.replaceDefault(this.resForm.script);
                this.formJs = new Function('return ' + scriptJs)();
                //暂停/恢复按钮
                if (self_.buttons && self_.buttons.length > 0) {
                    if (this.bpmInstDetail.bpmInst.status == "RUNNING") {
                        this.myStartBtn = ['doDiscard', 'delInst', 'supspendInst'];
                    } else if (this.bpmInstDetail.bpmInst.status == "SUPSPEND") {
                        this.myStartBtn = ['doDiscard', 'delInst', 'restoreInst'];
                    } else if (this.bpmInstDetail.bpmInst.status == "CANCEL") {
                        this.myStartBtn = ['delInst'];
                    }
                }
            })
        },
        doTracked() {
            let instId = this.bpmInst.instId;
            BpmInstTrackedApi.doTracked(instId).then(res => {
                uni.showToast({
                    title: res.message, duration: 2000, icon: 'none'
                });
                this.bpmInstDetail.tracked = res.data;
            })
        },
        retrieveTaskNode() {
            let data = {
                instId: this.bpmInst.instId,
                subject: this.bpmInst.subject,
                from: this.from
            }
            let url = '/views/modules/bpm/task/BpmInstTakeBack';
            url = url + '?data=' + JSON.stringify(data) + '&appId=' + this.appId
            uni.navigateTo({url: url})
        },
        getCurVm() {
            return this;
        },
        /*
        留言
         */
        showMsg() {
            let instId = this.bpmInst.instId;
            let data = {
                instId: instId,
            }
            let url = '/views/modules/bpm/task/BpmAmessage';
            url = url + '?data=' + JSON.stringify(data)
            uni.navigateTo({url: url})
        },
        /*
            审批历史
             */
        approvalHistory(btn) {
            uni.navigateTo({
                url: '/views/modules/bpm/task/BpmApprovalHistory?instId=' + this.bpmInst.instId + '&appId=' + this.appId
            })
        },
        flowChart(bth) {
            let self = this.parentVm;
            let formData = this.rxForms.getData();
            let conf = {
                data: {
                    instId: self.instId,
                    defId:self.bpmInst.defId,
                    formData: formData,
                    dialogOptions: {
                        title:this.i18n('bpmImage','流程图'),
                        okText: ''
                    },
                },
                curVm: this,
            };
            Dialog.openBpmImageView(conf, function (obj) {
            })
        },
    }
}
</script>

<style scoped>

</style>
